<template>
	<!-- 输入验证码 -->
	<view>
		<view class="navbar">
			<navigator open-type="navigateBack">
				<image src="/static/image/back.png"></image>
			</navigator>
		</view>
		<view class="logo">
			<image src="/static/logo.png"></image>
		</view>
		<view class="title">输入验证码</view>
		<view class="desc">在下方输入您的验证码</view>
		<u-message-input :value="code" :width="98" :maxlength="6" @finish="finish"></u-message-input>
		<view class="loginbtn" @click="login">登录</view>
	</view>
</template>

<script>
	const jyJPush = uni.requireNativePlugin('JY-JPushThird');
	export default {
		data() {
			return {
				phone:'',
				code:'',
				openid:'',
				device:''
			}
		},
		onLoad(options) {
			this.phone = options.phone;
			if(options.openid){
				this.openid = options.openid;
			}
			if(options.device){
				this.device = options.device;
			}
		},
		methods: {
			finish(e){
				this.code = e;
			},
			//登录
			login() {
				if (this.code == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '登录中...'
				})
				if(this.device === ''){
					this.$api.logincode({
						phone:this.phone,
						code:this.code
					}).then(res => {
						uni.setStorageSync('token',res.session_num);
						// 绑定设备号
						let deviceId = uni.getSystemInfoSync().deviceId;
						// #ifdef APP-PLUS
						jyJPush.setJYJPushAlias({
							userAlias: deviceId
						}, result=> {
							console.log(result,'userAlias');
						});
						// #endif
						this.$api.binddevice({device_id:deviceId}).then(deviceRes=>{
							uni.hideLoading();
							uni.showToast({
								title:'登录成功',
								icon:'none'
							})
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/index/index'
								})
							},1000)
						})
					})
				}else{
					let bindData = {};
					if(this.device === 'wx'){
						bindData = {
							phone:this.phone,
							open_id:this.openid
						}
					}else{
						bindData = {
							phone:this.phone,
							apple_openid:this.openid
						}
					}
					this.$api.bindphone(bindData).then(res => {
						// 绑定设备号
						let deviceId = uni.getSystemInfoSync().deviceId;
						this.$api.binddevice({device_id:deviceId}).then(deviceRes=>{
							uni.hideLoading();
							uni.showToast({
								title:'登录成功',
								icon:'none'
							})
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/index/index'
								})
							},1000)
						})
					})
				}
			}
		}
	}
</script>

<style lang="scss">
page{
	padding-bottom: 30rpx;
}
.navbar {
	width: 750rpx;
	height: 100rpx;
	z-index: 9999;
	position: fixed;
	top: 0;
	@include center;

	navigator {
		position: absolute;
		left: 30rpx;

		image {
			width: 36rpx;
			height: 40rpx;
		}
	}
}
.logo{
	width: 109rpx;
	height: 109rpx;
	border-radius: 20rpx;
	margin: 204rpx 572rpx 54rpx 69rpx;
	
	image{
		@include img
	}
}
.title{
	font-size: 50rpx;
	font-family: Source Han Sans CN;
	font-weight: 500;
	color: #0D0D0D;
	margin-left: 69rpx;
}
.desc{
	font-size: 28rpx;
	font-family: Source Han Sans CN;
	font-weight: 400;
	color: #595959;
	margin: 30rpx 0 100rpx 69rpx;
}
.loginbtn{
	margin: 100rpx 55rpx 81rpx;
	@include center
	position: relative;
	width: 640rpx;
	height: 90rpx;
	background: #FF773B;
	border-radius: 45rpx;
	font-size: 30rpx;
	font-family: Source Han Sans CN;
	font-weight: 500;
	color: #FFFFFF;
}
</style>
